
package net.sky.oa.service.system.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.sky.oa.dao.system.SysEmployeeDao;
import net.sky.oa.model.SysEmployee;
import net.sky.oa.service.system.SysEmployeeService;
import net.sky.oa.util.ConvertUtils;
import net.sky.oa.util.ReflectionUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htsoft.core.Constants;
import com.htsoft.core.service.impl.BaseServiceImpl;
import com.htsoft.core.util.StringUtil;
import com.htsoft.core.web.paging.PagingBean;
import com.htsoft.oa.dao.system.AppRoleDao;
import com.htsoft.oa.dao.system.AppUserDao;
import com.htsoft.oa.model.system.AppRole;
import com.htsoft.oa.model.system.AppUser;

@Service
@Transactional
public class SysEmployeeServiceImpl extends BaseServiceImpl<SysEmployee> implements SysEmployeeService {

	private SysEmployeeDao dao;
	private AppUserDao appUserDao;
	private AppRoleDao appRoleDao;

	@Resource
	public void setDao(SysEmployeeDao dao) {
		super.setDao(dao);
		this.dao = dao;
	}

	@Resource(name="appUserDao")
	public void setAppUserDao(AppUserDao appUserDao) {
		this.appUserDao = appUserDao;
	}
	
	@Resource(name="appRoleDao")
	public void setAppRoleDao(AppRoleDao appRoleDao) {
		this.appRoleDao = appRoleDao;
	}

	@Transactional(readOnly=true)
	public List<SysEmployee> findAllEmployee() {
		return this.dao.findAllEmployee();
	}
	
	public List<SysEmployee> findBySch(Long sch){
	    return this.dao.findBySch(sch);
	}
	public List<SysEmployee> findByDept(Long dept){
		return this.dao.findByDept(dept);
	}
	public List<SysEmployee> findByPost(Long post){
		return this.dao.findByPost(post);
	}
	
	public List<SysEmployee> getNewEmpl(PagingBean pb) {
		return this.dao.getNewEmpl(pb);
	}
	
	public List<SysEmployee> getBirthday() {
		return this.dao.getBirthday();
	}
	
	public SysEmployee save(SysEmployee entity, String appUserRoleIds) {
		 
		this.save(entity);
		
		AppUser appUser=appUserDao.findByUserName(entity.getUsername());
		 
		Set<AppRole> roles = new HashSet<AppRole>(appRoleDao.getAll()) ;//appUser.getRoles();
		
		if (StringUtils.isEmpty(appUserRoleIds)) {
			roles.clear();
		}else{
			List<Long> checkedIds = ConvertUtils.convertStringToList(appUserRoleIds);
			
			try {
				Iterator<AppRole> srcIterator = roles.iterator();
				while (srcIterator.hasNext()) {
					AppRole element = srcIterator.next();
					Long id = element.getRoleId();
	
					if (!checkedIds.contains(id)) {
						srcIterator.remove();
					}
				}
			} catch (Exception e) {
				throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
			}
		}
		
	    appUser.setRoles(roles);
		
		 appUserDao.save(appUser);
		 
		 return entity;
	}

	@Override
	public SysEmployee save(SysEmployee entity) {
		AppUser appUser=appUserDao.findByUserName(entity.getOldusername());
		if(null==appUser)
		{
			appUser = new AppUser();
			appUser.setDelFlag(Constants.FLAG_UNDELETED);
			if(entity.getPassword() != null){
				appUser.setPassword(StringUtil.encryptSha256(entity.getPassword()));
			}
		}else{
		}
		appUser.setUsername(entity.getUsername());
		Short s;
		if(entity.getStatus().equals(1)){//Integer 1,2
			s = 1;
		}else{
			s = 0;
		}
		appUser.setStatus(s);
	    
		if(entity.getStatus().equals(3)){
			appUser.setDelFlag(Constants.FLAG_DELETED);
		}
		appUserDao.save(appUser);
		 
		return super.save(entity);
	}
	
	public void saveEmpl(SysEmployee entity) {
		this.dao.save(entity);
	}
	
	//ids已逗号形式进行分割
	@Transactional(readOnly=true)
	public List<SysEmployee> findByIds(String ids) {
		List<SysEmployee> empList = new ArrayList<SysEmployee>();
		return dao.findByHql("from SysEmployee se where se.id in ("+ids+")");
	}
	
	public  List<Long> getSuper(final Long userId)
	{
		return dao.getSuper(userId);
	}
	//根据手机号查询员工
	public List<SysEmployee> findByMobile(String mobile) {
		return dao.findByMobile(mobile);
	}
	
	
}

